﻿using System.ServiceModel;
using NGeo.Yahoo.GeoPlanet.Json;

namespace NGeo.Yahoo.GeoPlanet
{
    public sealed class GeoPlanetClient : ClientBase<IInvokeGeoPlanetServices>, IConsumeGeoPlanet
    {
        public Place Place(int woeId, string appId, RequestView view = RequestView.Long)
        {
            var response = Channel.Place(woeId.ToString(), appId, view);
            return response.Result.ToPlace();
        }

        public Place Parent(int woeId, string appId, RequestView view = RequestView.Long)
        {
            var response = Channel.PlaceParent(woeId.ToString(), appId, view);
            return response.Result.ToPlace();
        }

        public Places Ancestors(int woeId, string appId, RequestView view = RequestView.Short)
        {
            var response = Channel.PlaceAncestors(woeId.ToString(), appId, view);
            var results = response.Results;
            if (results != null)
            {
                results.Items = results.JsonItems.ToPlaces();
                results.Items.Reverse();
            }
            return results;
        }

        public PlaceTypes Types(string appId, RequestView view = RequestView.Long)
        {
            var response = Channel.PlaceTypes(appId, view);
            var results = response.Results;
            if (results != null)
                results.Items = results.JsonItems.ToPlaceTypes();
            return results;
        }

        public Places Countries(string appId, RequestView view = RequestView.Short)
        {
            var response = Channel.Countries(appId, view);
            var results = response.Results;
            if (results != null)
                results.Items = results.JsonItems.ToPlaces();
            return results;
        }

        public ConcordanceResponse Concordance(ConcordanceNameSpace nameSpace, string id, string appId)
        {
            var response = Channel.Concordance(nameSpace.GetEnumMemberAttribute().Value, id, appId);
            response.ForNameSpace = nameSpace;
            return response;
        }

        public ConcordanceResponse Concordance(ConcordanceNameSpace nameSpace, int id, string appId)
        {
            var response = Channel.Concordance(nameSpace.GetEnumMemberAttribute().Value, id.ToString(), appId);
            response.ForNameSpace = nameSpace;
            return response;
        }

    }
}
